/* eslint-disable object-curly-newline */
import React, { Component } from 'react';
import { message } from 'antd';
import * as config from '../../utils/config';
import * as utils from '../../utils/utils';
import * as comFunc from './ComFunc';
import * as commonServices from '../../services/services';

export default (ChildComponent) => {
  return class Base extends Component {
    constructor(props) {
      super(props);
      this.state = {
        pageLoading: true,
        token: props.app.token,
        sTabId: this.handleConfigValue('key'),
        sModelsId: this.handleConfigValue('formId'), /* 获取配置数据用的modelId */
        formRoute: this.handleConfigValue('route'), /* 组件名： 路由名称 */
        masterConfig: {}, /* 主表配置 */
        masterShowConfig: [], /* 主表显示配置 */
        slaveConfig: {}, /* 从表配置 */
        slaveColumn: [], /* 显示的从表表头 */
        slaveShowConfig: [], /* 从表显示配置 */
        checkConfig: {}, /* 审核配置 */
        checkColumn: [], /* 显示的审核表表头 */
        gdsformconst: [], /* 获取配置常量 */
        gdsjurisdiction: [], /* 获取配置权限 */
        billnosetting: {}, /* 是否自动审核 */
        report: [], /* 打印模板 */
        masterData: {}, /* 主表数据 */
        slaveData: [], /* 从表数据 */
        dataTotal: {}, /* 合计字段 */
        checkData: [], /* 审核表数据 */
        masterFlag: false, /* 是否获取主表数据 */
        slaveFlag: false, /* 是否获取从表数据 */
        checkFlag: false, /* 是否获取审核表数据 */
        sId: '', /* 数据Id */
        disabled: false, /* 页面是否可编辑 */
      };
    }

    componentWillMount() {
      /* 获取配置 */
      this.handleConfigData();
    }

    componentWillReceiveProps(nextProps) {
      const { currentPane } = nextProps.app; /* 当前页签key */
      if (utils.isNotEmptyObject(currentPane)) {
        const {
          editTableRender,
          isRender,
          isRenderPart,
          showRender,
          showSlaveRender,
        } = currentPane;
        if (utils.isNotUndefined(isRender)) {
          this.setState({
            editTableRender,
            isRender,
            isRenderPart,
            showRender,
            showSlaveRender,
          });
        }
      }
    }

    shouldComponentUpdate(nextProps, nextState) {
      const currentKey = nextProps.app.currentPane.key; /* 当前页签key */
      const { sId, masterFlag, slaveFlag, checkFlag, copyToData } = nextState;
      const flag = sId !== '' || utils.isNotEmptyObject(copyToData) ? masterFlag && slaveFlag && checkFlag : masterFlag;
      return currentKey === nextState.sTabId && flag;
    }

    /* 获取配置数据用的sTabId,formId,formRoute */
    handleConfigValue = (value) => {
      const { secondppopupPane, ppopupPane, currentPane } = this.props.app;
      const data = utils.isNotEmptyObject(secondppopupPane)
        ? secondppopupPane[value]
        : utils.isNotEmptyObject(ppopupPane)
          ? ppopupPane[value] : currentPane[value];
      return data;
    };

    /* 获取配置 */
    handleConfigData = async () => {
      const { token, sModelsId } = this.state;
      const { copyToData } = this.props.app.currentPane;
      const configUrl = `${config.server_host}business/getModelBysId/${sModelsId}?sModelsId=${sModelsId}`;
      const configReturn = (await commonServices.getService(token, configUrl)).data;
      if (configReturn.code === 1) {
        const [configData] = configReturn.dataset.rows;
        this.handleGetData(configData, copyToData);
      } else {
        message.error(configReturn.msg);
      }
    };

    /* 获取数据 */
    handleGetData = (configData, copyToData) => {
      const { currentPane } = this.props.app; /* 当前页签数据 */
      const { sParentId, sFilterOrderBy, pages, bFilter, isRender, currentIndex } = currentPane; /*   上一条下一条信息   */
      const masterCopyData = utils.isEmptyObject(copyToData) ? {} : copyToData.masterData;
      const slaveCopyData = utils.isEmptyObject(copyToData) ? [] : copyToData.slaveData;
      const sId = currentPane.checkedId !== undefined ? currentPane.checkedId : ''; /* 数据Id */
      const { formData } = configData;
      let { masterShowConfig, slaveColumn, checkColumn, slaveShowConfig } = this.state;
      const masterConfig = formData.filter(item => item.bGrd === false)[0]; /* bGrd代表是否是表格 */
      masterShowConfig = comFunc.getMasterShowConfig(masterConfig, masterShowConfig);
      const slaveConfig = formData.filter(item => item.bGrd && item.sTbName !== 'sysbillcheckresult')[0];
      slaveColumn = comFunc.getHeaderConfig(slaveConfig, slaveColumn);
      slaveShowConfig = comFunc.getSlaveShowConfig(slaveConfig, slaveShowConfig);
      const checkConfig = formData.filter(item => item.bGrd && item.sTbName === 'sysbillcheckresult')[0];
      checkColumn = comFunc.getHeaderConfig(checkConfig, checkColumn);
      if (utils.isNotEmptyStr(sId) || utils.isNotEmptyObject(masterCopyData)) {
        const obj = {
          isRender,
          masterConfig,
          slaveConfig,
          checkConfig,
          sId,
          masterData: masterCopyData,
          slaveData: slaveCopyData,
          configData,
          copyToData,
          masterShowConfig,
          slaveColumn,
          slaveShowConfig,
          checkColumn,
          sParentId,
          sListFormmasterId: sParentId,
          sFilterOrderBy,
          pages,
          bFilter,
          currentIndex,
        };
        this.handleGetAllData(obj);
      } else {
        const masterData = comFunc.getDefaultMasterData(masterShowConfig);
        /* 新增页面时 */
        this.setState({
          ...configData,
          masterConfig,
          masterShowConfig,
          masterData,
          masterFlag: true,
          slaveConfig,
          slaveColumn,
          slaveShowConfig,
          checkConfig,
          checkColumn,
          pageLoading: false,
          sListFormmasterId: sParentId,
          sFilterOrderBy,
          pages,
          bFilter,
          currentIndex,
        });
      }
    };

    /* 获取数据 */
    handleGetAllData = (obj) => {
      /*   有些state要重新设置的   */
      this.setState({
        ...obj.configData,
        copyToData: obj.copyToData,
        masterFlag: false,
        slaveFlag: false,
        checkFlag: false,
        sId: obj.sId,
        pageLoading: false,
        disabled: utils.isEmptyObject(obj.copyToData),
        isRender: obj.isRender,
        isChangeHandleTypeUpdate: obj.isChangeHandleTypeUpdate,
        masterConfig: obj.masterConfig,
        masterShowConfig: obj.masterShowConfig,
        slaveConfig: obj.slaveConfig,
        slaveColumn: obj.slaveColumn,
        slaveShowConfig: obj.slaveShowConfig,
        checkConfig: obj.checkConfig,
        checkColumn: obj.checkColumn,
        sListFormmasterId: obj.sListFormmasterId,
        sFilterOrderBy: obj.sFilterOrderBy,
        pages: obj.pages,
        bFilter: obj.bFilter,
        currentIndex: obj.currentIndex,
      });
      /* 获取主表数据 */
      this.handleGetMasterData(obj);
      /* 获取从表数据和合计字段 */
      this.handleGetSlaveData(obj);
      /* 获取审核表数据 */
      this.handleGetCheckData(obj);
    };

    /* 获取主表数据 */
    handleGetMasterData = async (obj) => {
      if (utils.isEmptyObject(obj.masterData) && utils.isNotEmptyStr(obj.sId)) {
        const { masterConfig, sId } = obj;
        const { token, sModelsId } = this.state;
        const mastersId = masterConfig.sId;
        const masterUrl = `${config.server_host}business/getBusinessDataByFormcustomId/${mastersId}?sModelsId=${sModelsId}`;
        const value = { sId, pageSize: '', pageNum: '' };
        const masterReturn = (await commonServices.postValueService(token, value, masterUrl)).data;
        if (masterReturn.code === 1) {
          const [masterData] = masterReturn.dataset.rows[0].dataSet;
          this.setState({ masterData, masterFlag: true });
        } else {
          message.error(masterReturn.msg);
        }
      } else {
        this.setState({ masterData: obj.masterData, masterFlag: true });
      }
    };

    /* 获取从表数据 */
    handleGetSlaveData = async (obj) => {
      if (utils.isEmptyArr(obj.slaveData) && utils.isNotEmptyStr(obj.sId)) {
        const { slaveConfig, sId } = obj;
        const { token, sModelsId } = this.state;
        const slavesId = slaveConfig.sId;
        const slaveUrl = `${config.server_host}business/getBusinessDataByFormcustomId/${slavesId}?sModelsId=${sModelsId}`;
        const value = { sSqlCondition: { sParentId: sId } };
        const slaveReturn = (await commonServices.postValueService(token, value, slaveUrl)).data;
        if (slaveReturn.code === 1) {
          const slaveData = slaveReturn.dataset.rows[0].dataSet;
          const { sumSet } = slaveReturn.dataset.rows[0];
          const dataTotal = sumSet !== undefined ? sumSet[0] : {};
          this.setState({ slaveData, dataTotal, slaveFlag: true });
        } else {
          message.error(slaveReturn.msg);
        }
      } else {
        this.setState({ slaveData: obj.slaveData, dataTotal: obj.dataTotal, slaveFlag: true });
      }
    };

    /* 获取审核表数据 */
    handleGetCheckData = async (obj) => {
      if (utils.isEmptyArr(obj.checkData) && utils.isNotEmptyStr(obj.sId)) {
        const { checkConfig, sId } = obj;
        const { token, sModelsId } = this.state;
        const checksId = checkConfig.sId;
        const checkUrl = `${config.server_host}business/getBusinessDataByFormcustomId/${checksId}?sModelsId=${sModelsId}`;
        const value = { sSqlCondition: { sParentId: sId } };
        const checkReturn = (await commonServices.postValueService(token, value, checkUrl)).data;
        if (checkReturn.code === 1) {
          const checkData = checkReturn.dataset.rows[0].dataSet;
          this.setState({ checkData, checkFlag: true });
        } else {
          message.error(checkReturn.msg);
        }
      } else {
        this.setState({ checkData: obj.checkData, checkFlag: true });
      }
    };

    render() {
      return (
        <ChildComponent
          {...this.props}
          {...this.state}
          onGetAllData={this.handleGetAllData}
        />
      );
    }
  };
};

